from typing import Optional
from pydantic import BaseModel, Field, validator
import re


class UniversalVideoRequest(BaseModel):
    """通用视频链接请求模型"""
    url: str = Field(..., description="视频链接（支持Facebook、Instagram、Twitter/X、YouTube等平台）")
    quality: Optional[str] = Field("best", description="视频质量偏好")
    
    @validator('url')
    def validate_url(cls, v):
        """验证URL格式"""
        if not v:
            raise ValueError("URL不能为空")
        
        # 基本URL格式验证
        url_pattern = r'^https?://'
        if not re.match(url_pattern, v):
            raise ValueError("URL必须以http://或https://开头")
        
        # 检查是否为支持的平台
        supported_patterns = [
            # Facebook patterns
            r'https?://(www\.|m\.)?facebook\.com/(watch|videos|posts|reel)',
            r'https?://(www\.)?fb\.watch/',
            # Instagram patterns  
            r'https?://(www\.)?instagram\.com/(p|reel|tv)/',
            # Twitter/X patterns
            r'https?://(www\.)?(twitter\.com|x\.com|mobile\.twitter\.com)/.+/status/\d+',
            r'https?://(www\.)?(twitter\.com|x\.com)/i/web/status/\d+',
            # YouTube patterns
            r'https?://(www\.)?(youtube\.com|youtu\.be)/',
            r'https?://(www\.)?m\.youtube\.com/',
            r'https?://youtube\.com/shorts/',
        ]
        
        is_supported = any(re.search(pattern, v) for pattern in supported_patterns)
        
        if not is_supported:
            raise ValueError(
                "不支持的视频链接格式。支持的平台：\n"
                "- Facebook: facebook.com/watch, fb.watch\n"
                "- Instagram: instagram.com/p, instagram.com/reel\n"
                "- Twitter/X: twitter.com/status, x.com/status\n"
                "- YouTube: youtube.com, youtu.be, m.youtube.com, youtube.com/shorts"
            )
        
        return v
    
    @validator('quality')
    def validate_quality(cls, v):
        """验证质量参数"""
        if v is None:
            return "best"
        
        valid_qualities = ["1080p", "720p", "480p", "360p", "240p", "best", "worst"]
        if v not in valid_qualities:
            raise ValueError(f"不支持的质量选项: {v}. 支持: {', '.join(valid_qualities)}")
        return v


class UniversalVideoInfoRequest(BaseModel):
    """通用视频信息请求模型"""
    url: str = Field(..., description="视频链接（支持Facebook、Instagram、Twitter/X、YouTube等平台）")
    
    @validator('url')
    def validate_url(cls, v):
        """验证URL格式"""
        if not v:
            raise ValueError("URL不能为空")
        
        # 基本URL格式验证
        url_pattern = r'^https?://'
        if not re.match(url_pattern, v):
            raise ValueError("URL必须以http://或https://开头")
        
        # 检查是否为支持的平台
        supported_patterns = [
            # Facebook patterns
            r'https?://(www\.|m\.)?facebook\.com/(watch|videos|posts|reel)',
            r'https?://(www\.)?fb\.watch/',
            # Instagram patterns  
            r'https?://(www\.)?instagram\.com/(p|reel|tv)/',
            # Twitter/X patterns
            r'https?://(www\.)?(twitter\.com|x\.com|mobile\.twitter\.com)/.+/status/\d+',
            r'https?://(www\.)?(twitter\.com|x\.com)/i/web/status/\d+',
            # YouTube patterns
            r'https?://(www\.)?(youtube\.com|youtu\.be)/',
            r'https?://(www\.)?m\.youtube\.com/',
            r'https?://youtube\.com/shorts/',
        ]
        
        is_supported = any(re.search(pattern, v) for pattern in supported_patterns)
        
        if not is_supported:
            raise ValueError(
                "不支持的视频链接格式。支持的平台：\n"
                "- Facebook: facebook.com/watch, fb.watch\n"
                "- Instagram: instagram.com/p, instagram.com/reel\n"  
                "- Twitter/X: twitter.com/status, x.com/status\n"
                "- YouTube: youtube.com, youtu.be, m.youtube.com, youtube.com/shorts"
            )
        
        return v 